﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Objects;
using System.Collections.Generic;
using BAL;
using System.IO;

public partial class Admin_UCControls_UCChuDe_Update : System.Web.UI.UserControl
{
    public static string srtPath;

    public static ChuDe ItemChuDe;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int temp;
            try
            {
                temp =Convert.ToInt16(Request.QueryString["IdChuDe"].ToString());
            }
            catch
            {
                temp = -1;
            }


            ItemChuDe = new ChuDe();

            if (temp > 0)
            {
           
                ItemChuDe.Id = Convert.ToInt16(Request.QueryString["IdChuDe"].ToString());

                ItemChuDe = BAL.BALChuDe.GetByIdChuDe(ItemChuDe.Id);

                txtTenChuDe.Text = ItemChuDe.TenChuDe.ToString();

                txtNoiDung.Text = ItemChuDe.GhiChu.ToString();


                string pathfile = PathImage();

                imgChuDe.ImageUrl = pathfile + ItemChuDe.DuongDan;
            }
           
        }
       
       
    }

    public string PathImage()
    {
        return srtPath = "~/images/Generic Card/ChuDe/";
    }


    protected void btnLuu_Click(object sender, EventArgs e)
    {
        int item = 0;
        if (CheckNull())
        {
            if (ItemChuDe.Id == 0)
            {
                if (FUDuongDan.HasFile)
                {
                    if (CheckFileType(FUDuongDan.FileName))
                    {
                        srtPath = PathImage() + FUDuongDan.FileName;

                        FUDuongDan.SaveAs(MapPath(srtPath));


                        item = BALChuDe.Insert_ChuDe(txtTenChuDe.Text, FUDuongDan.FileName.ToString(), txtNoiDung.Text, 0);
                        lblTenFile.Visible = true;
                        lblTenFile.Text = "File Uploaded: " + FUDuongDan.FileName;
                        imgChuDe.ImageUrl = srtPath;
                    }
                    else
                    {
                        lblTenFile.Visible = true;
                        lblTenFile.Text = "File Uploaded: " + " Invalid format.";
                    }

                }
                else
                {
                    lblTenFile.Visible = true;
                    lblTenFile.Text = "No File Uploaded.";
                }

            }
            else
            {
                // kiem tra co doi hinh hay ko, hay doi chu de
                if (!FUDuongDan.FileName.Equals(""))
                {
                    item = BALChuDe.Update_ChuDe(Int32.Parse(ItemChuDe.Id.ToString()), txtTenChuDe.Text, FUDuongDan.FileName.ToString(), txtNoiDung.Text, 1, Int32.Parse(ItemChuDe.SapXep.ToString()));
                    lblTenFile.Visible = true;
                    lblTenFile.Text = "File Uploaded: " + ItemChuDe.DuongDan.ToString();
                    imgChuDe.ImageUrl = srtPath;

                }
                else
                {
                    item = BALChuDe.Update_ChuDe(Int32.Parse(ItemChuDe.Id.ToString()), txtTenChuDe.Text, ItemChuDe.DuongDan.ToString(), txtNoiDung.Text, 1, Int32.Parse(ItemChuDe.SapXep.ToString()));
                    imgChuDe.ImageUrl = PathImage() + ItemChuDe.DuongDan.ToString();
                }

            }
        }

        if (item > 0)
        {
            lblThongbao.Visible = true;
            lblThongbao.Text = "Cập nhật thành công";

        }
        else
        {
            lblThongbao.Visible = true;
            lblThongbao.Text = "Cập nhật thất bại";
        }
    }

    bool CheckFileType(string fileName)
    {

        string ext = Path.GetExtension(fileName);

        switch (ext.ToLower())
        {

            case ".gif":

                return true;

            case ".png":

                return true;

            case ".jpg":

                return true;

            case ".jpeg":

                return true;

            default:

                return false;

        }

    }

  
    protected void btnQuayve_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Admin/ChuDe.aspx");
    }

    public bool CheckNull()
    {
        bool flag = true;
        VisibleLable();

        if (txtTenChuDe.Text == "")
        {
            lblTenChuDe.Visible = true;
            txtTenChuDe.Focus();
            flag = false;
        }

        if (txtNoiDung.Text == "")
        {
            lblNoidung.Visible = true;
            txtNoiDung.Focus();
            flag = false;
        }
       
        return flag;
    }

    public void VisibleLable()
    {
        lblTenChuDe.Visible = false;
        lblNoidung.Visible = false;
    }

 
}
